<?php
/**
 *	description:ZMAXWECHAT valid类实现文件
 *  author：min.zhang
 *  Email:zhang19min88@163.com
 *	Url:http://www.zmax99.com
 *  copyright:南宁市程序人软件科技有限责任公司保留所有权利
 *  date:2016-04-28
 *  @license GNU General Public License version 3, or later
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.helper');
jimport('joomla.log.log');
JLog::addLogger(array('text_file'=>'com_zmaxwechat.log.php' ) ,
                                JLOG::ALL ,
                                array('zmax' ,'jerror'));

class zmaxwechatValid
{
	protected $token; //组件后台获得的token
	protected $state; //得到组件后台的state
	
	public function __construct()
	{
		$params = JComponentHelper::getParams('com_zmaxwechat');
		$token = $params->get("token","zmax99");
		$state = $params->get("state","0");
		$this->token = $token;
		$this->state = $state;
	}
	
	public function valid()
    {
		$app = JFactory::getApplication();
		@$echoStr =$app->input->get("echostr" ,"","STRING");
        //@$echoStr = $_GET["echostr"];
        if($this->checkSignature())
		{
        		echo $echoStr;
		exit;
        }
		return false;
    }
	
	private function checkSignature()
	{
		$app = JFactory::getApplication();
		$signature = $app->input->get("signature","","STRING");
        //$signature = $_GET["signature"]; //得到签名
		
		$timestamp = $app->input->get("timestamp","","STRING");
        //$timestamp = $_GET["timestamp"]; //得到时间戳
		
		$nonce = $app->input->get("nonce","","STRING");
        //$nonce = $_GET["nonce"]; //是否是一次性的
		
        		
		$tmpArr = array($this->token, $timestamp, $nonce);
		//使用字符串排序
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr ); //使用shal运算，得到一个临时的签名
		
		//JLog::add("tmpStr=".$tmpStr ,JLOG::WARNING ,'zmax');
		//JLog::add("sign=".$signature ,JLOG::WARNING ,'zmax');
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

?>